package appx.craft.asd;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import appx.craft.asd.bean.CountryBean;
import appx.craft.asd.ui.MainActivity;
import appx.craft.asd.utils.Const;
import appx.craft.asd.utils.JSONParser;
import appx.craft.asd.utils.Utility;


// TODO: Auto-generated Javadoc
/**
 * The Class ChooseActivity.
 */
public class ChooseActivity extends Activity {

	
	/** The tag. */
	final String TAG = ChooseActivity.class.getCanonicalName();
	
	/** The m beanof codes. */
	ArrayList<CountryBean> mBeanofCodes = new ArrayList<CountryBean>();
	
	 // JSON Node names
	
 	/** The Constant TAG_DIAL_CODE. */
 	static final String TAG_DIAL_CODE = "dial_code";
	
	
	/** The Constant TAG_COUNTRY_NAME. */
	static final String TAG_COUNTRY_NAME = "name";
	
	
	/** The Constant TAG_CODE_ARRAY. */
	static final String TAG_CODE_ARRAY = "country";
	
	
	/** The m progress dialog. */
	ProgressDialog mProgressDialog;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "[ onCreate ] ");
		mProgressDialog = new ProgressDialog(ChooseActivity.this);
		mProgressDialog.setMessage("Please wait....");
		mProgressDialog.setIndeterminate(true);
		mProgressDialog.setCancelable(false);
		if(!Utility.verifySerializableFile(Const.FILE_CODE_LIST)){
			new RetrieveIconsList().execute();
		}else{
			startActivity(new Intent(ChooseActivity.this,MainActivity.class));
			finish();
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		super.onStart();
		Log.d(TAG, "[ onStart ] ");
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		Log.d(TAG, "[ onStop ] ");
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "[ onPause ] ");
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "[ onResume ] ");
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "[ onDestroy ] ");
	}
	
	
	/**
	 * The Class RetrieveIconsList.
	 */
	class RetrieveIconsList extends AsyncTask<String,Void, Integer>{
		
		/* (non-Javadoc)
		 * @see android.os.AsyncTask#onPreExecute()
		 */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			mProgressDialog.show();
		}
		
		/* (non-Javadoc)
		 * @see android.os.AsyncTask#doInBackground(Params[])
		 */
		@Override
		protected Integer doInBackground(String... params) {
			int errorNo = 0;
			
			// JSONArray
		    JSONArray mCountryJsonArray = null;
		     // Creating JSON Parser instance
	        JSONParser jParser = new JSONParser();
	        // getting JSON string from local file
	        JSONObject mJson = jParser.getJSONFromLocal(ChooseActivity.this,R.raw.jsoncode);
	        
	        try {
	        	
            
	            mCountryJsonArray = mJson.getJSONArray(TAG_CODE_ARRAY);
	        
	            CountryBean mBeanofDialoCodeList;
	        
	            for(int i = 0; i < mCountryJsonArray.length(); i++){
	            	
	            	mBeanofDialoCodeList = new CountryBean();
	                JSONObject mObjectMessage = mCountryJsonArray.getJSONObject(i);
	                
	                String mDialCode = mObjectMessage.getString(TAG_DIAL_CODE);
	                String mCounryName = mObjectMessage.getString(TAG_COUNTRY_NAME);
	                
	                mBeanofDialoCodeList.setCode(mDialCode);
	                mBeanofDialoCodeList.setName(mCounryName);
	                
	                
	                mBeanofCodes.add(mBeanofDialoCodeList);
	            }
	            
	            Utility.writeObject(Const.FILE_CODE_LIST,mBeanofCodes);
	        } catch (JSONException e) {
	            e.printStackTrace();
	        }
	        
			return errorNo;
		}
		
		/* (non-Javadoc)
		 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
		 */
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			mProgressDialog.dismiss();
			if(result == 0){
				startActivity(new Intent(ChooseActivity.this,MainActivity.class));
				finish();
			}
		}
		
	}
}
